package cn.darkhorse.ticket.basis.service.impl;

import cn.darkhorse.exception.ServiceException;
import cn.darkhorse.ticket.basis.dao.TicketProductDao;
import cn.darkhorse.ticket.basis.mapper.TicketProductMapper;
import cn.darkhorse.ticket.basis.pojo.TicketProduct;
import cn.darkhorse.ticket.basis.service.TicketProductService;
import cn.darkhorse.ticket.constants.TicketConstants;
import cn.darkhorse.ticket.production.pojo.TicketInfo;
import cn.darkhorse.ticket.production.pojo.TicketProductionPlanProductInfo;
import cn.darkhorse.ticket.production.service.TicketInfoService;
import cn.darkhorse.ticket.production.service.TicketProductionPlanProductInfoService;
import cn.darkhorse.ticket.service.IRepeatCheckService;
import cn.darkhorse.ticket.basis.vo.TicketProductVO;
import cn.darkhorse.ticket.store.pojo.TicketWarehousing;
import cn.darkhorse.ticket.store.pojo.TicketWarehousingSub;
import cn.darkhorse.ticket.store.service.TicketWarehousingService;
import cn.darkhorse.ticket.store.service.TicketWarehousingSubService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 产品信息 服务实现类
 * </p>
 *
 * @author wangxinghui
 * @since 2024-01-09
 */
@Service
public class TicketProductServiceImpl extends ServiceImpl<TicketProductMapper, TicketProduct> implements TicketProductService {



	@Resource
	private TicketProductDao ticketProductDao;
	@Resource
	private IRepeatCheckService<TicketProduct> repeatCheckService;

	@Override
	public String repeat(TicketProduct product) {
		String code = null;
		if(null==product.getId()){
			code = repeatCheckService.repeatAutoCodeAndName(this, product.getId(), TicketConstants.BUSINESS_TYPE_CP,
					product.getCode(), product.getName(), product.getIsAutomatically());
		}else {
			repeatCheckService.repeatName(this,product.getId(),product.getName());
		}
		return code;
	}

	@Resource
	private TicketProductionPlanProductInfoService planProductInfoService;
	@Resource
	private TicketInfoService ticketInfoService;
	@Resource
	private TicketWarehousingSubService warehousingSubService;
	/**
	 * 引用
	 * @param id
	 * @return  false  引用  true 未引用
	 */
	@Override
	public boolean isQuote(Long id) {
		LambdaQueryWrapper<TicketProductionPlanProductInfo> planProductInfoWrapper = new LambdaQueryWrapper<>();
		planProductInfoWrapper.eq(TicketProductionPlanProductInfo::getProductId,id);
		long count = planProductInfoService.count(planProductInfoWrapper);
		if(count > 0){
			//throw new ServiceException("该产品已关联计划，无法删除！");
			return false;
		}
		LambdaQueryWrapper<TicketInfo> ticketInfoWrapper = new LambdaQueryWrapper<>();
		ticketInfoWrapper.eq(TicketInfo::getProductId,id);
		long count2 = ticketInfoService.count(ticketInfoWrapper);
		if(count2 > 0){
			//throw new ServiceException("该产品已关联工单，无法删除！");
			return false;
		}
		LambdaQueryWrapper<TicketWarehousingSub> PlanProductInfo = new LambdaQueryWrapper<>();
		PlanProductInfo.eq(TicketWarehousingSub::getProductId,id);
		long count3 = warehousingSubService.count(PlanProductInfo);
		if(count3 > 0){
			throw new ServiceException("该产品已关联入库单，无法删除！");
		}
		return true;
	}

	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@Override
	public TicketProductVO view(Long id) {
		TicketProductVO productVO = ticketProductDao.view(id);
		return productVO;
	}

	/**
	 * 是否管理工艺路线
	 *
	 * @param id
	 * @return
	 */
	@Override
	public boolean hasProduce(Long id) {
		LambdaQueryWrapper<TicketProduct> wrapper=new LambdaQueryWrapper<>();
		wrapper.eq(TicketProduct::getId,id);
		wrapper.isNotNull(TicketProduct::getRoutingCode);
		wrapper.ne(TicketProduct::getRoutingCode,"");
		long count = count(wrapper);
		if(count>0){
			return true;
		}
		return false;
	}


}
